accessingSubstrings
Strings Sandbox MichS Revised

Accessing Substrings

Problem
=======

How to (e.g.) extract characters 1  to 3 (abc) from the variable named
buffer and store the result in a variable.

Often you'll  find the need to  extract a part of  an existing string,
into another string. awk provides  this functionally with the substr()
function:

  result = substr(var, start [, length])

In  the expression  above, substr()  returns a  portion of  the string
variable named  'var' beginning at  the position named 'start'  in the
2nd argument, with  an optional 'length' for the 3rd  argument (if the
length argument is not supplied,  substr() returns all characters from
'start' till the end of the input string).

If for  instance, you had a  string named 'buffer' with  the following
characters:

  buffer = "abcdef"

and you needed to extract the  1st three characters, you would specify
a 'start' position of 1 (character  indexes always start at 1),  and a
'length' of 3 as shown in the example below.

Solution
========

The following code returns _abc_.

  buffer = "abcdef"
  result = substr(buffer, 1, 3)

Author
======

Michael Sanders

    function AccessingSubstrings(buffer, result) {
	buffer = "abcdef"
	result = substr(buffer, 1, 3)
	print result == "abc"
    }

